package com.ruoyi.config;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * @ClassName ElasticsearchConfig
 * @Description
 * @Author huangshun
 * @Date 2021/1/21 0021 下午 3:41
 */
@Configuration
public class ElasticsearchConfig {
    /**
     * localhost:9300 写在配置文件中就可以了
     */

    @Value("${es.host}")
    private String host;
    @Value("${es.username}")
    private String username;
    @Value("${es.password}")
    private String password;
    @Value("${es.connectTimeOut}")
    private Integer MAX_CONN_TOTAL;
    @Value("${es.maxConnectPerRoute}")
    private Integer MAX_CONN_PER_ROUTE;

    @Bean
    public RestClientBuilder elasticsearchClient() {

        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

//        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "yiqi2020"));
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

        RestClientBuilder builder = RestClient.builder(
//                new HttpHost("115.159.5.99",9200,"http"))
                new HttpHost(host,9200,"http"))
                //new HttpHost("192.168.3.106",9200,"http"))
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(
                            HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider);
                    }
                });
        builder.setHttpClientConfigCallback(httpClientBuilder -> {
            httpClientBuilder.setMaxConnTotal(MAX_CONN_TOTAL);
            httpClientBuilder.setMaxConnPerRoute(MAX_CONN_PER_ROUTE);
            httpClientBuilder.setKeepAliveStrategy((httpResponse, httpContext) -> Duration.ofMinutes(2).toMillis());
            httpClientBuilder.disableAuthCaching();
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            return httpClientBuilder;
        });
        return builder;
    }


    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }
}
